package com.zhoushihao.class0808;

public class MyInteger {
    public static String toBinaryString(int n) {
        String result = "";
        while (n > 0) {
            result = n % 2 + result;
            n = n / 2;
        }
        return result;
    }

    public static int parseInt(String s, int radix) {
        int result = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
                result = result * radix + (s.charAt(i) - '0');
            } else if (s.charAt(i) >= 'A' && s.charAt(i) <= 'F') {
                result = result * radix + (s.charAt(i) - 'A' + 10);
            } else if (s.charAt(i) >= 'a' && s.charAt(i) <= 'f')
                result = result * radix + (s.charAt(i) - 'a' + 10);
        }
        return result;
    }

    public static String toHexString(int n) {
        String result = "";
        while (n > 0) {
            int a = n % 16;
            if(a >= 10) {
                result = (char) (a - 10 + 'a') + result;
            } else {
                result = a + result;
            }
            n = n / 16;
        }
        return result;
    }
}
